/**
 * <p>Project: Components</p>
 * <p>Package: com.patac.hmi.ui.corepage.util</p>
 * <p>File: LogUtil.java</p>
 * <p>Date: 2016/08/08/17:21.</p>
 */
package com.pactera.asmp.server.common.logprint.utils;


import com.pactera.asmp.server.common.constants.MagicNumberConstant;

public class LogUtil {

    /**
     * Priority constant for the println method; use Log.v.
     */
    public static final int VERBOSE = 2;

    /**
     * Priority constant for the println method; use Log.d.
     */
    public static final int DEBUG = 3;

    /**
     * Priority constant for the println method; use Log.i.
     */
    public static final int INFO = 4;

    /**
     * Priority constant for the println method; use Log.w.
     */
    public static final int WARN = 5;

    /**
     * Priority constant for the println method; use Log.e.
     */
    public static final int ERROR = 6;

    /**
     * Priority constant for the println method.
     */
    public static final int ASSERT = 7;
    private static final String PREFIX = "[ASMP SERVER] ";
    private static String sTag = StringUtils.EMPTY;
    private static int sCurrentLevel = VERBOSE;
    private static final int MAX_LOG_WORDS_LINE = 2000;

    public static final void init(String tag, int level) {
        sTag = tag;
        sCurrentLevel = level;
    }

    public static final void d(String msg) {
        if (sCurrentLevel <= DEBUG) {
            Log.d(sTag, toString(msg));
        }
    }

    public static final void d(Object... msg) {
        if (sCurrentLevel <= DEBUG) {
            Log.i(sTag, toString(StringUtils.append(msg)));
        }
    }

    public static final void i(String msg) {
        if (sCurrentLevel <= INFO) {
            Log.i(sTag, toString(msg));
        }
    }

    public static final void i(Object... msg) {
        if (sCurrentLevel <= DEBUG) {
            Log.i(sTag, toString(StringUtils.append(msg)));
        }
    }

    public static final void w(String msg) {
        if (sCurrentLevel <= WARN) {
            Log.w(sTag, toString(msg));
        }
    }

    public static final void e(String msg) {
        if (sCurrentLevel <= ERROR) {
            Log.e(sTag, toString(msg));
        }
    }

    public static final void e(Throwable e) {
        if (sCurrentLevel <= ERROR) {
            e.printStackTrace();
            Log.e(sTag, e.getLocalizedMessage());
        }
    }

    public static final void e(Object... msg) {
        if (sCurrentLevel <= DEBUG) {
            Log.e(sTag, toString(StringUtils.append(msg)));
        }
    }

    public static final void e(String msg, Throwable e) {
        if (sCurrentLevel <= ERROR) {
            e.printStackTrace();
            Log.e(sTag, toString(msg), e);
        }
    }


    private static String toString(String content) {
       // StringBuffer sb = new StringBuffer(PREFIX);
        return content;
    }

    /**
     * to cut log with 2048 words every land
     * @param content
     * @return
     */
    private static void toString(String content, boolean flag) {
        try {
            StringBuffer allBuffer = new StringBuffer(content);
            int length = allBuffer.length();
            if (length >= MagicNumberConstant.TEN_THOUSAND) {
                return;
            }
            for (int i = length; i > MAX_LOG_WORDS_LINE; i = MathUtil.safeSubtract(length, MAX_LOG_WORDS_LINE)) {
                if (allBuffer.length() > MAX_LOG_WORDS_LINE) {
                    i(allBuffer.substring(0, MAX_LOG_WORDS_LINE).toString());
                    allBuffer = new StringBuffer(allBuffer.substring(MAX_LOG_WORDS_LINE));
                }
            }
            i(allBuffer.toString());
        } catch (OutOfMemoryError e) {
            e(StringUtils.append("Long Log Info OutOfMemoryError"));
        } finally {

        }
    }

    /**
     * d 2048 words
     * @param flag
     * @param msg
     */
    public static final void d(boolean flag, String msg) {
        if (sCurrentLevel <= DEBUG) {
            toString(msg, flag);
        }
    }

    /**
     * d 2048 words
     * @param flag
     * @param msg
     */
    public static final void d(boolean flag, Object... msg) {
        if (sCurrentLevel <= DEBUG) {
            toString(StringUtils.append(msg), flag);
        }
    }

    /**
     * i 2048 words
     * @param flag
     * @param msg
     */
    public static final void i(boolean flag, String msg) {
        if (sCurrentLevel <= INFO) {
            toString(msg, flag);
        }
    }

    /**
     * i 2048 words
     * @param flag
     * @param msg
     */
    public static final void i(boolean flag, Object... msg) {
        if (sCurrentLevel <= DEBUG) {
            toString(StringUtils.append(msg), flag);
        }
    }

    /**
     * w 2048 words
     * @param flag
     * @param msg
     */
    public static final void w(boolean flag, String msg) {
        if (sCurrentLevel <= WARN) {
            toString(msg, flag);
        }
    }

    /**
     * e 2048 words
     * @param flag
     * @param msg
     */
    public static final void e(boolean flag, String msg) {
        if (sCurrentLevel <= ERROR) {
            toString(msg, flag);
        }
    }

    /**
     * e 2048 words
     * @param flag
     * @param msg
     */
    public static final void e(boolean flag, Object... msg) {
        if (sCurrentLevel <= DEBUG) {
            toString(StringUtils.append(msg), flag);
        }
    }

    /**
     * e 2048 words
     * @param flag
     * @param msg
     * @param e
     */
    public static final void e(boolean flag, String msg, Throwable e) {
        if (sCurrentLevel <= ERROR) {
            e.printStackTrace();
            toString(msg, flag);
            Log.e(sTag, StringUtils.DASH, e);
        }
    }

    /**
     * v 2048 words
     * @param flag
     * @param msg
     */
    public static final void v(boolean flag, String msg) {
        if (sCurrentLevel <= VERBOSE) {
            toString(msg, flag);
        }
    }
}
